package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;

import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import jdk.nashorn.internal.ir.annotations.Reference;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
// @RefreshScope // 热部署
public class UserController {

    @Autowired
    private UserService userService;

//    @Value("${pattern.dateformat}")
//     private String dateformat;

    @Autowired
    private PatternProperties patternProperties;

    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) throws IOException {
        String truth = request.getHeader("Truth");

        log.info("请求头信息:{}",truth);


        return userService.queryById(id);
    }


    @GetMapping("/now")
    public String now(){
        System.out.println(patternProperties.getDemo());
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
    }

    @GetMapping("/demo")
    public PatternProperties patternProperties(){
        return patternProperties;
    }
}
